/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.smart.hawk.utilities;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;

/**
 * Class manager Combo box SmartHawk
 *
 * @author <h1>Andres Felipe Escobar Lopez - andresfescobar92@gmail.com</h1>
 */
public class HawkComboBoxMgr<C> {

    private static String DEFAULT_EMPTY_VALUE = "Seleccione";
    private JComboBox combo;
    private List<C> data;
    private String emptyValue = null;

    /**
     * Component for using an object ComboBox starting
     *
     * @param combo
     */
    public HawkComboBoxMgr(JComboBox combo) {
        this.combo = combo;
    }

    /**
     * Fill in the combo with the list object information
     *
     * @param data
     */
    public void setData(List<C> data) {

        if (data == null) {
            removeAllItems();
            return;
        }

        if (data.isEmpty()) {
            removeAllItems();
            return;
        }

        removeAllItems();

        this.data = new ArrayList<>(data);
        for (C object : data) {
            this.combo.addItem(object);
        }

    }

    /**
     * Returns object item selected
     *
     * @return
     */
    public C getSelectedItem() {
        if ((emptyValue != null && emptyValue.equals(this.combo.getSelectedItem())) || DEFAULT_EMPTY_VALUE.equals(this.combo.getSelectedItem())) {
            return null;
        }
        return (C) this.combo.getSelectedItem();
    }

    /**
     * Returns the Combo inserted in the list
     *
     * @return
     */
    public List<C> getData() {
        return this.data;
    }

    /**
     * Remove all items from collection
     */
    public void removeAllItems() {
        if (this.data != null) {
            this.data.clear();
        }
        this.combo.removeAllItems();
        this.combo.addItem(emptyValue == null ? DEFAULT_EMPTY_VALUE : emptyValue);
    }

    /**
     * Sets value when component is empty
     *
     * @param value
     */
    public void setEmptyValue(String value) {
        emptyValue = value;
    }
    
    /**
     * Refresh component
     */
    public void refresh() {
        if (this.data != null) {
            for (C c : this.data) {
                if (c instanceof String) {
                    if (emptyValue == null) {
                        if (DEFAULT_EMPTY_VALUE.equals(c)) {
                            this.data.remove(c);
                        }
                    } else {
                        if (emptyValue.equals(c)) {
                            this.data.remove(c);
                        }
                    }
                }
            }
            List<C> refreshList = new ArrayList<>(this.data);
            setData(refreshList);
        }
    }
   
}
